<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教研组" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入教研组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动主题" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入活动主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="daterangeActivityTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="状态" prop="state">-->
      <!--        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.sys_yes_no"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="审核状态" prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.jy_audit_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['research:jySchoolResearch:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jySchoolResearchList" stripe border>
      <el-table-column label="序号" type="index" align="center" width="50"/>
      <el-table-column label="教研组" align="center" prop="groupName"/>
      <el-table-column label="活动主题" align="center" prop="subject"/>
      <el-table-column label="活动时间" align="center" prop="activityTime" width="180"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jy_audit_state" :value="scope.row.auditState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="suditMsg" show-tooltip-when-overflow/>
      <el-table-column label="备注" align="center" prop="remark" show-tooltip-when-overflow/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['research:jySchoolResearch:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['research:jySchoolResearch:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改 校本教研对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
        <el-divider content-position="left">教研活动</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="教研组" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入教研组名称" readOnly @focus="$refs.jyResearchGroupSelect.show()"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动时间" prop="activityTime">
              <el-date-picker clearable
                              v-model="form.activityTime"
                              style="width: 100%"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择活动时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动主题" prop="subject">
              <el-input v-model="form.subject" type="textarea" placeholder="请输入活动主题"/>
            </el-form-item>
          </el-col>

          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="状态" prop="state">-->
          <!--              <el-select v-model="form.state" placeholder="请选择状态" style="width: 100%">-->
          <!--                <el-option-->
          <!--                  v-for="dict in dict.type.sys_yes_no"-->
          <!--                  :key="dict.value"-->
          <!--                  :label="dict.label"-->
          <!--                  :value="dict.value"-->
          <!--                ></el-option>-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="left">活动内容</el-divider>
      <el-form ref="updateForm" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="实到人员" prop="teacherNames">
              <el-input v-model="form.teacherNames" type="textarea" placeholder="请输入实到人员" readOnly @focus="this.handleTeacherSelectShow"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="教研内容" prop="groupName">
              <FormFileUpload ref="contentList" :businessType="businessType" childType="contentList" :businessId="form.id"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="活动照片" prop="groupName">
              <FormFileUpload ref="imgList" type="img" :businessType="businessType" childType="imgList" :businessId="form.id"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="left">审核信息</el-divider>
      <el-form ref="auditForm" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="auditState">
              <el-select v-model="form.auditState" placeholder="请选择审核状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.jy_audit_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核意见" prop="suditMsg">
              <el-input v-model="form.suditMsg" type="textarea" placeholder="请输入审核意见"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <JyResearchGroupSelect ref="jyResearchGroupSelect" required @selection-change="handleGroupSelectChange"/>
    <JyGroupTeacherSelect ref="jyGroupTeacherSelect" multiple @selection-change="handleTeacherSelectChange"/>
  </div>
</template>

<script>
  import {listJySchoolResearch, getJySchoolResearch, delJySchoolResearch, addJySchoolResearch, updateJySchoolResearch} from "@/api/research/jySchoolResearch"
  import FormFileUpload from '@/components/FileUpload/FormFileUpload.vue'
  import JyResearchGroupSelect from '@/views/research/jyResearchGroup/JyResearchGroupSelect.vue'
  import JyGroupTeacherSelect from '@/views/research/jyTeacher/JyGroupTeacherSelect.vue'

  export default {
    name: "JySchoolResearch",
    dicts: ['jy_audit_state', 'sys_yes_no'],
    components: {FormFileUpload, JyResearchGroupSelect, JyGroupTeacherSelect},
    data() {
      return {
        businessType: "jy_school_research",
        // 遮罩层
        loading: true,
        // 选中数组
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        //  校本教研表格数据
        jySchoolResearchList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 备注时间范围
        daterangeActivityTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          groupId: null,
          groupName: null,
          subject: null,
          activityTime: null,
          state: null,
          auditState: null,
          suditMsg: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          deptId: [
            {required: true, message: "部门Id不能为空", trigger: "blur"}
          ],
          delFlag: [
            {required: true, message: "删除标志不能为空", trigger: "blur"}
          ],
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询 校本教研列表 */
      getList() {
        this.loading = true
        this.queryParams.params = {}
        if (null != this.daterangeActivityTime && '' != this.daterangeActivityTime) {
          this.queryParams.params["beginActivityTime"] = this.daterangeActivityTime[0]
          this.queryParams.params["endActivityTime"] = this.daterangeActivityTime[1]
        }
        listJySchoolResearch(this.queryParams).then(response => {
          this.jySchoolResearchList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          groupId: null,
          groupName: null,
          subject: null,
          activityTime: null,
          state: null,
          auditState: null,
          suditMsg: null,
          deptId: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          teacherNames: null
        }
        this.resetForm("form")
        if (this.$refs.imgList) {
          this.$refs.imgList.clearFileList()
        }
        if (this.$refs.contentList) {
          this.$refs.contentList.clearFileList()
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeActivityTime = []
        this.resetForm("queryForm")
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加 校本教研"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id
        getJySchoolResearch(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改 校本教研"
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.form.imgList = this.$refs.imgList.getFileList()
        this.form.contentList = this.$refs.contentList.getFileList()
        this.$refs["addForm"].validate(valid => {
          if (valid) {
            this.$modal.confirm('保存后数据不可更改，是否确认进行保存？')
              .then(response => {
                this.$modal.loading()
                return this.form.id != null ? updateJySchoolResearch(this.form) : addJySchoolResearch(this.form)
              })
              .then(response => {
                this.$modal.closeLoading()
                this.$modal.msgSuccess("保存成功")
                this.open = false
                this.getList()
              }).catch(err => {
              this.$modal.closeLoading()
            });
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id
        this.$modal.confirm('是否确认删除 校本教研编号为"' + ids + '"的数据项？').then(function () {
          return delJySchoolResearch(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('research/jySchoolResearch/export', {
          ...this.queryParams
        }, `jySchoolResearch_${new Date().getTime()}.xlsx`)
      },
      handleGroupSelectChange(select) {//教研组选择
        this.form.groupId = select.id
        this.form.groupName = select.name
      },
      handleTeacherSelectChange(select) {//实到老师选择
        let teacherNames = select.map(item => item.name).join()
        console.log("sd", teacherNames)
        this.form.teacherNames = teacherNames
      },
      handleTeacherSelectShow() {
        if (!this.form.groupId) {
          this.$modal.msgError("请选择教研组")
          return
        }
        let option = {id: this.form.groupId}
        this.$refs.jyGroupTeacherSelect.show(option)
      },
    }
  }
</script>
